package com.epfresh.delegate;

import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.bean.Market;
import com.epfresh.global.BaseActivity;
import com.epfresh.views.dialog.BaseDialogView;
import com.epfresh.views.tag.FlowLayout;
import com.epfresh.views.tag.TagAdapter;
import com.epfresh.views.tag.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author wangzhanghuan
 * @version 1.0
 * @date create 17-2-21
 * @Description
 */

public class CategoryGoodsMarketFilterDelegate extends BaseDialogView implements View.OnClickListener {
    BaseActivity activity;

    TextView tvClear;
    TextView tvSure;
    TagFlowLayout filterTag;


    View vParent;


    TagAdapter<Market> adapter;
    List<Market> names = new ArrayList<>();

    OnFilter onFilter;

    View v;
    View sv;

    int w;

    public CategoryGoodsMarketFilterDelegate(BaseActivity activity) {
        super(activity);
        this.activity = activity;
        init();
        initView();
    }

    @Override
    public void initBefore() {
        setGravity(Gravity.TOP);
    }

    @Override
    protected void initAnim() {
        super.initAnim();
    }

    private void init() {

        v = LayoutInflater.from(context).inflate(R.layout.view_category_maket_filter, contentContainer);

        ViewGroup.LayoutParams lp0 = v.getLayoutParams();
        lp0.width = LocalDisplay.SCREEN_WIDTH_PIXELS;
        v.setLayoutParams(lp0);
        sv = v.findViewById(R.id.sv);

        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) rootView.getLayoutParams();
        lp.topMargin = context.getResources().getDimensionPixelSize(R.dimen.action_bar_height);
        setCancelable(true);

    }

    public OnFilter getOnFilter() {
        return onFilter;
    }

    public void setOnFilter(OnFilter onFilter) {
        this.onFilter = onFilter;
    }

    public View findViewById(int id) {
        return v.findViewById(id);
    }


    private void initView() {

        tvClear = (TextView) findViewById(R.id.tv_market_clear);
        tvSure = (TextView) findViewById(R.id.tv_market_sure);

        vParent = findViewById(R.id.rl_market_filter);
        filterTag = (TagFlowLayout) findViewById(R.id.ft_market_filter);

        tvClear.setOnClickListener(this);
        tvSure.setOnClickListener(this);

        w = LocalDisplay.SCREEN_WIDTH_PIXELS - LocalDisplay.dp2px(40);
        w /= 3;
        adapter = new TagAdapter<Market>(names) {

            @Override
            public View getView(FlowLayout parent, int position, Market comm) {
                TextView tv = (TextView) LayoutInflater.from(activity).inflate(R.layout.category_market_tag,
                        parent, false);


                ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) tv.getLayoutParams();
                lp.width = w;
//                lp.leftMargin = LocalDisplay.dp2px(10);
//                lp.topMargin = LocalDisplay.dp2px(6);

                Log.e("width", "w:" + w);
                Log.e("width", "lp.leftMargin:" + lp.leftMargin);
                Log.e("width", "lp.topMargin:" + lp.topMargin);
//                lp.rightMargin = 0;
//                lp.bottomMargin = 0;
//                lp.height = LocalDisplay.dp2px(30);
                tv.setLayoutParams(lp);
                tv.setText(comm.getShortName());
                return tv;
            }
        };

        filterTag.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {
                updateSelected(selectPosSet);
            }
        });

        filterTag.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                if (position == 0) {
                    adapter.setSelectedList(0);
                    return true;
                }
                return false;
            }
        });

        filterTag.setAdapter(adapter);
    }

    private void updateSelected(Set<Integer> selectPosSet) {

        if (selectPosSet != null) {

            if (((selectPosSet.size() == 1 && selectPosSet.contains(0))) || selectPosSet.size() == 0) {
                adapter.setSelectedList(0);
            } else {
                selectPosSet.remove(0);
                adapter.setSelectedList(selectPosSet);

            }
        } else {
            adapter.setSelectedList(0);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_market_clear:
                clear();
                break;
            case R.id.tv_market_sure:
                sure();
                break;

        }
    }

    private void sure() {


        Set<Integer> set = filterTag.getSelectedList();
        List<String> name = null;
        StringBuilder sb = new StringBuilder();
        int cnt = 0;
        for (Integer i : set) {
            Market p = names.get(i);
            String id = p.getId();

            if (id != null && !"".equals(id)) {
                if (name == null) {
                    name = new ArrayList<>();
                }

                name.add(id);
                cnt ++;
            }
            String n = p.getShortName();
            if (n != null) {
                sb.append(n);
            }
        }

        if (onFilter != null) {
            String title = sb.toString();
            if (cnt > 1){
                title = "已选"+cnt+"个市场";
            }
            onFilter.onFilter(set, name, title);
        }
        dismiss();
    }

    private void clear() {
        adapter.setSelectedList(0);
    }


    public void show(List<Market> names, Set<Integer> set) {

        this.names.clear();
        Market p = new Market();
        p.setName("全部");
        this.names.add(p);

        if (names != null && names.size() > 0)
            this.names.addAll(names);

        if (this.names.size() == 1 || set == null || set.size() == 0) {
            adapter.setSelectedList(0);
//            adapter.notifyDataChanged();
        } else {
            adapter.setSelectedList(set);
//            adapter.notifyDataChanged();
        }

        ViewGroup.LayoutParams lp = sv.getLayoutParams();

        int size = this.names.size();
        int line = size / 3 + (size % 3 > 0 ? 1 : 0);

//        T.toast(line + "");
        int d = LocalDisplay.dp2px(30);
        if (line < 4) {
            lp.height = 3 * d + LocalDisplay.dp2px(6) * 4;
//            T.toast(line + "--1-" + lp.height);
        } else if (line * d > LocalDisplay.SCREEN_HEIGHT_PIXELS * 5 / 13) {
            lp.height = LocalDisplay.SCREEN_HEIGHT_PIXELS * 5 / 13;
//            T.toast(line + "--2-" + lp.height);
        } else {
            lp.height = line * d + LocalDisplay.dp2px(6) * (line + 1);
//            T.toast(line + "--3-" + lp.height);
        }
        sv.setLayoutParams(lp);
        show();
    }

    public interface OnFilter {
        void onFilter(Set<Integer> set, List<String> names, String name);
    }

}
